﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AltimilliaLibrary.State;
using Microsoft.Xna.Framework;

namespace AltimilliaLibrary.Intelligence
{
    public class SpriteAction
    {
        WalkDirection direction;
        int tilestoMove;
        Point destinationPositon;
        public bool finished { get; set; }
        public bool IsStarted { get; private set; }

        public delegate void SpriteActionFinished(SpriteAction action);
        public event SpriteActionFinished ActionFinished;

        public SpriteAction(WalkDirection direction, int tilestomove)
        {
            this.direction = direction;
            this.tilestoMove = tilestomove;
        }

        public void StartUp(Sprite sprite)
        {
          if (this.direction == WalkDirection.WalkRight)
          {
            destinationPositon = new Point(sprite.TilePosition.X + tilestoMove, sprite.TilePosition.Y);
          }
          else if(this.direction == WalkDirection.WalkDown)
          {
            destinationPositon = new Point(sprite.TilePosition.X, sprite.TilePosition.Y + tilestoMove);
          }
          else if (this.direction == WalkDirection.WalkLeft)
          {
            destinationPositon = new Point(sprite.TilePosition.X - tilestoMove, sprite.TilePosition.Y);
          }
          else if (this.direction == WalkDirection.WalkUp)
          {
            destinationPositon = new Point(sprite.TilePosition.X, sprite.TilePosition.Y - tilestoMove);
          }
            finished = false;
            IsStarted = true;
        }

        public void RunAction(Sprite sprite)
        {
            if (sprite.TilePosition != destinationPositon)
            {
                sprite.ChangeDirection(this.direction);
                sprite.MoveDirection(this.direction);
            }
            else
            {
                finished = true;
                IsStarted = false;
                if (ActionFinished != null)
                {
                    ActionFinished(this);
                }
            }
        }
    }
}
